package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.xd.study.R;
import com.xd.study.utility.MyImageAdapter;

/**
 * Created by DGF on 2015/4/29.
 */
public class GalleryActivity extends Activity {

    private ImageSwitcher imageSwitcher;
    private int[] res;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        Gallery gallery = (Gallery)this.findViewById(R.id.gallery);
        imageSwitcher = (ImageSwitcher)this.findViewById(R.id.imageswitcher);

        /**
         * 数据源
         */
        int[] res = {R.drawable.gallery1, R.drawable.gallery2, R.drawable.gallery3, R.drawable.gallery4,
                     R.drawable.gallery5, R.drawable.gallery6, R.drawable.gallery7, R.drawable.gallery8,
                     R.drawable.gallery9, R.drawable.gallery10, R.drawable.gallery11, R.drawable.gallery12};

        /**
         * 初始化适配器
         */
        final MyImageAdapter myImageAdapter = new MyImageAdapter(this, res);

        /**
         * 加载适配器
         */
        gallery.setAdapter(myImageAdapter);

        /**
         * 在ImageSwitcher中展示当前选中的图片
         */
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                imageSwitcher.setBackgroundResource((int)myImageAdapter.getItem(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        /**
         * ImageSwitcher加载图片
         */
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(GalleryActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//等比例缩放，大图展示
                return imageView;
            }
        });

        /**
         * 设置ImageSwitcher的动画效果
         * 淡入，淡出
         */
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    }
}
